app.controller('bpmTaskFormController', ['$scope', 'httpWrapService', '$state', '$stateParams', 'uiGridConstants', 'GridFactory', 'AlertFactory', 'dialogs', '$window', '$ocLazyLoad',
    function ($scope, httpWrapService, $state, $stateParams, uiGridConstants, GridFactory, AlertFactory, dialogs, $window, $ocLazyLoad) {

        $scope.Alert = AlertFactory.newInstance();
        $scope.map = {};
        $scope.eventMap = {};
        $scope.taskId = $stateParams.taskId;

        $scope.formData = {};
        $scope.other = {};

        $scope.tempForm = {};
        $scope.status = {};
        $scope.status.priorityStatus = true;
        $scope.status.dueDateStatus = true;
        $scope.status.delegationStateStatus = true;

        $scope.initStatus = function(){
        	$scope.status.priorityStatus = true;
        	$scope.status.dueDateStatus = true;
        	$scope.status.delegationStateStatus = true;
        }
        
//        $scope.$watch('tempForm.dueDate', function(){
//        	if($scope.tempForm.dueDate) {
//        		changeProperties('dueDate', $scope.task.id, $scope.tempForm.dueDate)
//        	}
//        });
//        
        $scope.changeProperties = function(type, taskId, value){
        	httpWrapService.post('bpm/task/property/' + taskId + '.do', {
        		propertyName : type,
        		value : value
        	}).then(function (res) {
        		$scope.initStatus();
        		$scope.load(taskId);
            }, function(){
            	$scope.initStatus();
            });
        }
        
        $scope.delegate = function(taskId, delegateUserId){
        	httpWrapService.post('bpm/task/delegate/' + taskId + '.do', {
        		delegateUserId : delegateUserId
        	}).then(function (res) {
        		$scope.initStatus();
        		$scope.load($scope.taskId);
            }, function(){
            	$scope.initStatus();
            });
        }
        
        
        $scope.load = function (taskId) {
            httpWrapService.post('bpm/task/getform/' + taskId + '.do', {}).then(function (res) {
                $scope.map = res.data.map;
                $scope.task = res.data.map.task;
                $scope.loadCommont();
            });
        }

        $scope.loadCommont = function () {
            // 读取意见列表
            httpWrapService.post('bpm/comment/list.do', {
                processInstanceId: $scope.task.processInstanceId,
                taskId: $scope.task.id
            }).then(function (res2) {
                $scope.eventMap = res2.data.map;
            });
        }

        $scope.load($scope.taskId);

        /**
         * 签收
         */
        $scope.claim = function (taskId) {
            httpWrapService.post('bpm/task/claim/' + taskId + '.do', {}).then(function (res) {
                dialogs.notify('提示', res.data.msg, {size: 'md'});
            }, function (res) {
                dialogs.error('错误', res.data.msg, {size: 'md'});
            });
        }
        /**
         * 完成任务
         */
        $scope.complete = function (taskId) {
            httpWrapService.post('bpm/task/complete/' + taskId + '.do', {
                formData: $scope.formData
            }).then(function (res) {
                dialogs.notify('提示', '任务成功完成', {size: 'md'});
            }, function (res) {
                dialogs.error('错误', res.data.msg, {size: 'md'});
            });
        }

        /**
         * 添加附件
         */
        $scope.addAttachment = function (taskId, processInstanceId) {
            var data = {
                taskId: taskId,
                processInstanceId: processInstanceId
            }
            $ocLazyLoad.load([
                'tpl/bpm/manager/task/addAttachmentModal.js'
            ]).then(function () {
                var dlg = dialogs.create('tpl\/bpm\/manager\/task\/addAttachmentModal.html', 'bpmAddAttachmentModalController', data, {
                    size: 'md',
                    keyboard: true,
                    backdrop: true,
                    windowClass: 'my-class'
                });
                dlg.result.then(function() {
                    $scope.load($scope.taskId);
                });
            });
        }

        $scope.deleteAttachment = function(attachmentId, attachmentName) {
            httpWrapService.confirmPost('bpm/attachment/delete/' + attachmentId + '.do', {}, '确认删除附件[' + attachmentName + ']？').then(function (res) {
                $scope.load($scope.taskId);
            }, function (res) {
                dialogs.notify('错误', res.data.msg, {size: 'sm'});
            });
        };
        /**
         * 保存意见
         */
        $scope.saveComment = function (taskId) {
            httpWrapService.post('bpm/comment/save/' + taskId + '.do', {
                processInstanceId: $scope.task.processInstanceId,
                message: $scope.other.comment
            }).then(function (res) {
                $scope.loadCommont();
            }, function (res) {
                dialogs.error('错误', res.data.msg, {size: 'md'});
            });
        }
        $scope.canle = function () {
            // 后退到列表页面
            $window.history.back();
        }

    }]);




